package com.campus.controller;

import com.campus.entity.ResultBody;
import com.campus.entity.Schedule;
import com.campus.entity.vo.ScheduleVO;
import com.campus.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author Alaskaboo
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;
    /**
     * 添加计划
     */
    @RequestMapping("/add")
    public ResultBody addSchedule(Schedule schedule) {
        return scheduleService.add(schedule);
    }
    @RequestMapping("/del")
    public ResultBody delSchedule(@RequestParam("id")String id){

        return scheduleService.delSchedule(id);
    }
    @RequestMapping("/query")
    public ResultBody querySchedule(@RequestParam("account")String account,@RequestParam("time")String timestamp){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long time = 0;
        try {
            time = sf.parse(timestamp).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return scheduleService.query(account,time);
    }
    //查询当月的任务
    @RequestMapping("/queryInMonth")
    public ResultBody queryInMonth(@RequestParam("account")String account,@RequestParam("year")String year,@RequestParam("month")String month){

        return scheduleService.queryInMonth(account,year,month);
    }
    @RequestMapping("/update")
    public ResultBody update( ScheduleVO vo){
        Schedule schedule = new Schedule(vo);
        return scheduleService.update(schedule);
    }
}
